package com.sxy.web.response; /**
 * @author 宋鑫宇
 * @version 1.0
 * 相应字节数据：设置字节数据的响应体
 */

import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/resp4")
public class ResponseDemo04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.读取文件
        FileInputStream fis = new FileInputStream("C:\\Users\\Administrator\\Pictures\\燕理.jpg");

        //2.获取response字节输出流
        ServletOutputStream os = response.getOutputStream();

        //3.完成copy
//        byte[] buff = new byte[1024];
//        int len = 0;
//        while ((len = fis.read(buff))!=-1){
//            os.write(buff,0,len);
//        }
        //使用IOUtils工具类来简化写法
        IOUtils.copy(fis,os);
        fis.close();

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
